<template>
    <div class="mybody_">
        <div class="mycentered">
            <div class="myheader_">
                <div class="_back"><a href="#/user" style="text-decoration: none;">&lt;</a></div>
                <div class="mine_">
                    <span>实名认证</span>
                </div>
            </div>
            <el-form ref="form" :model="studentData" size="normal" :rules="rules" class="idcard_">
                <el-form-item prop="realName">
                    <el-input placeholder="请输入姓名" clearable v-model="studentData.realName" :disabled="isEditing" />
                </el-form-item>
                <el-form-item prop="idCard">
                    <el-input placeholder="请输入身份证号" v-model="studentData.idCard" :disabled="isEditing"/>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" style="width: 100%" v-if="studentData.idCardType == 1" disabled>已实名</el-button>
                    <el-button type="primary" style="width: 100%" v-else @click="handleSearch">确认实名</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button style="width: 100%" @click="gotoUser">返回</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>
  
<script>
import { jwtDecode } from 'jwt-decode';

export default {
    name: "Login",
    name: 'LoginView',
    data() {
        return {
            
            form: {
                realName:"",
                idCard:""
            },
            rules: {
                realName: [
                    { required: true, message: '请输入姓名', trigger: 'blur' },
                    { pattern: /^[\u4e00-\u9fa5]+$/, message: '姓名只能输入中文', trigger: 'blur' }
                ],
                idCard: [
                    {
                        pattern: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,
                        message: '身份证号码格式不正确',
                        trigger: 'blur'
                    }
                ],
            },
            studentData: {
                realName:"",
                idCard:""
            },
            isEditing: false,
        };
    },
    methods: {
        gotoUser() {
            // 跳转注册页面
            this.$router.push('/user');
        },
        
        //更改学生实名状态
        handleSearch() {
            const jwt = localStorage.getItem('jwt');
        if (jwt) {
            const decoded = jwtDecode(jwt);
            const userId = decoded.id; // 确保你的token结构中有id字段
            const params = {
                id:userId,
                realName: this.form.realName,
                idCard: this.form.idCard,
            };
            // 将表单数据作为请求参数发送给后端
            this.$http.put('/api/students/idCards', params,{
                    headers: {
                        'Content-Type': 'application/json'
                    }
                })
                .then(response => {
                    if(response.data.code = "1"){
                        this.$message.success("实名认证成功");
                    }
                    else{
                        this.$message.error(response.data.message);
                    }
                })
                .catch(() => {
                    this.$message.error("请求错误");
                });
            }
        },

    },
    mounted() {
        const jwt = localStorage.getItem('jwt');
        if (jwt) {
            const decoded = jwtDecode(jwt);
            const userId = decoded.id; // 确保你的token结构中有id字段
            this.$http.get("/api/students/queryOneStudentById/" + userId).then(response => {  /*刷新页面*/
                this.studentData = response.data.data;
                if(this.studentData.idCardType == 1){
                    this.isEditing = true;
                }
            }).catch(() => {
                this.$message.error("请求错误");
            });
            // this.$http.get("/api/orders/queryOneStudentOrder/" + userId).then(response => {  /*刷新页面*/
            //     this.orderData = response.data.data.rows;
            // }).catch(() => {
            //     this.$message.error("请求错误");
            // });
            // 将导入的本地JSON文件赋值给regionOptions

        }
    },
};
</script>
  
<style>
@import "@/style/my.css";
</style>